//基础库，放一些工具，公共的方法（继承、扩展、单例）
var tool={
    //继承
    inherit:function(target,origin){    //target:目标对象；origin:源对象
        var F=function(){};
        F.prototype=origin.prototype;
        target.prototype=new F();
        target.prototype.constructor=target;
    },

    //扩展
    extends:function(origin){
        var target=function(){
            //继承属性
            origin.apply(this,arguments);
            //return this;
        }

        //原型继承
        this.inherit(target,origin);
        return target;
    },

    //单例
    single:function(origin){
        var singlResult=(function(){
            var instance;
            return function(){
                if(typeof instance == 'object'){    //如果这个条件成立，就说明是第二次new了，就不需要再创建了，要使用第一次的
                    return instance;
                }

                origin && origin.apply(this,arguments);
                instance=this;
            }
        })();
        

        //原型继承
        origin && this.inherit(singlResult,origin);
        return singlResult;
    }
};

/* function Square(x,y,width,height){
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height;
}
Square.prototype.collide=function(){
    console.log('collide');
} */

/* function Food(){
}

tool.inherit(Food,Square);
var f=new Food();
f.collide(); */


/* var Food=tool.extends(Square);
var f=new Food(10,10,100,200);
var f1=new Food(10,10,100,200);

f.collide();
console.log(f.x,f.y,f.width,f.height); */


/* var SnakeHead=tool.single(Square);
var s1=new SnakeHead(10,20,100,200);
var s2=new SnakeHead(20,10,200,100);
console.log(s1==s2); */